package com.exercise;

import javax.swing.tree.TreeNode;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-10
 * Time: 19:18
 */

//https://leetcode.cn/problems/balanced-binary-tree/submissions/394368737/
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
//public class Solution {
//    public boolean isBalanced(TreeNode root) {
//        return height(root) >= 0;
//    }
//    //减少调用的次数
//    public int height(TreeNode root) {
//        if (root == null) {
//            return 0;
//        }
//        int leftHeight = height(root.left);
//        int rightHeight = height(root.right);
//        if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
//            return -1;
//        } else {
//            return Math.max(leftHeight, rightHeight) + 1;
//        }
//    }
//}